package com.hu.bgp.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.RootPanel;
import com.hu.bgp.client.widgets.GameTypeTable;
import com.hu.bgp.client.widgets.GameTypeTableAdmin;
import com.hu.bgp.client.widgets.GameTypeTableUser;
import com.hu.bgp.framework.clientapi.BoardGameService;
import com.hu.bgp.framework.clientapi.BoardGameServiceAsync;
import com.hu.bgp.framework.clientapi.FailureHandler;
import com.hu.bgp.framework.clientapi.FailureHandlingCallback;
import com.hu.bgp.framework.clientapi.ServerToClientMessage;

import com.hu.bgp.shared.BgpAdminService;
import com.hu.bgp.shared.BgpAdminServiceAsync;
import com.hu.bgp.shared.BgpService;
import com.hu.bgp.shared.BgpServiceAsync;
import com.hu.bgp.shared.data.ClientGameType;
import com.hu.bgp.shared.data.ClientGameTypeCollection;

/**
 * De normale GUI voor het selecteren van een spelletje.
 * 
 * @author ejvos
 */
public class PlayerClient implements EntryPoint
{
	
	
	private BgpServiceAsync bgpService;
	private BgpAdminServiceAsync bgpAdminService = GWT.create(BgpAdminService.class);
	private final Button startButton = new Button("Start selected game type");
	private final Anchor adminButton = new Anchor("Admin");
	private final Button loadButton = new Button("Load selected game type");
	private final Button unloadButton = new Button("Unload selected game type");
	private final GameTypeTableUser gameTypeTable = new GameTypeTableUser();
	private final GameTypeTableAdmin gameTypeTableAdmin = new GameTypeTableAdmin();

	public void onModuleLoad()
	{
		// get a binding to the servlet
		bgpService = GWT.create(BgpService.class);
		ServiceDefTarget target = (ServiceDefTarget) bgpService;
		// zie ook BgpConfiguration.getPlayerClientUrlPrefix
		target.setServiceEntryPoint("/bgp/bgpService");
		loadadminEntryies();
		// grab a collection of gametypes
		bgpService.getClientGameTypeCollection(new AsyncCallback<ClientGameTypeCollection>()
		{
			public void onSuccess(ClientGameTypeCollection result)
			{
				for (ClientGameType gameType : result)
				{
					gameTypeTable.addGameRow(gameType);
				}
			}

			public void onFailure(Throwable caught) {
				Window.alert("failed to get Game types");
				
			}
		});
		
	
		loadButton.addClickHandler(new ClickHandler(){

			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				bgpAdminService.activateGame(gameTypeTableAdmin.getSelection(), new AsyncCallback<Void>(
						){

							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub
								
							}

							public void onSuccess(Void result) {
								// TODO Auto-generated method stub
								//loadadminEntryies();
								Window.alert("Succes!");
							}

						});
			}
			
		});
		startButton.addClickHandler(new StartHandler());
		adminButton.addClickHandler(new AdminHandler());
		RootPanel.get("gameTypeTable").add(gameTypeTable);
		RootPanel.get("actionButtons").add(startButton);
		RootPanel.get("adminButton").add(adminButton);
	}

	private class StartHandler implements ClickHandler
	{
		public void onClick(ClickEvent event)
		{
			//FIXME
			//TODO
		    new GameLoader().loadGame(gameTypeTable.getSelection());
		}
		
		
	}
	
	   public static native void push(String s) /*-{
       $wnd.RawGWT = s;
    }-*/;
	   
	private class AdminHandler implements ClickHandler
	{
		public void onClick(ClickEvent event)
		{
			RootPanel gameTypeTable = RootPanel.get("gameTypeTable");
			RootPanel actionButtons = RootPanel.get("actionButtons");
			gameTypeTable.getElement().setInnerText("");
			actionButtons.getElement().setInnerText("");
			gameTypeTable.add(gameTypeTableAdmin);
			
			loadButton.setStyleName("displayInline");
			unloadButton.setStyleName("displayInline");
			actionButtons.add(loadButton);
			actionButtons.add(unloadButton);

		}
	}
	//TODO QUICK DIRTY HACK
	private void loadadminEntryies()
	{
		ServiceDefTarget target = (ServiceDefTarget) bgpAdminService;
		// zie ook BgpConfiguration.getPlayerClientUrlPrefix
		target.setServiceEntryPoint("/bgp/admin");
		
		bgpAdminService.getClientGameTypeCollection(new AsyncCallback<ClientGameTypeCollection>()
				{
					public void onSuccess(ClientGameTypeCollection result)
					{
						for (ClientGameType gameType : result)
						{
							gameTypeTableAdmin.addGameRow(gameType);
						}
					}

					public void onFailure(Throwable caught) {
						Window.alert("Failed to load gametypes for admin table!");
						
					}
				});
	}
	

}
